//
//  SearchFriendView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/14.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "SearchFriendView.h"

@interface SearchFriendView()

@property (nonatomic, weak)   id<SearchFriendViewDelegate>delegate;
@property (nonatomic, strong) UITextField *account_field;
@property (nonatomic, strong) UIButton *bgView;

@end
@implementation SearchFriendView

+ (SearchFriendView *)defaultSearch
{
    static SearchFriendView *search = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        search = [[self alloc]init];
    });
    
    return search;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = RGBA(0, 0, 0, 0.1);
        
        self.frame = [UIScreen mainScreen].bounds;
        
        [self instanceSearchFriendView];
        
        [self registKeyBoardNotofication];
        
    }
    return self;
}

- (void)instanceSearchFriendView
{
    _bgView = [UIButton new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 1.5, SCREEN_HEIGHT / 3));
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    _account_field = [UITextField new];
    _account_field.layer.masksToBounds = YES;
    _account_field.layer.borderWidth = 2.0;
    _account_field.layer.borderColor = [RGB(220, 220, 220) CGColor];
    [_bgView addSubview:_account_field];
    [_account_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_bgView.mas_centerY);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.textColor = RGB(88, 88, 88);
    titleLabel.text = @"请输入好友手机号码查找";
    [_bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_account_field.mas_top).mas_equalTo(-10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(20);
    }];
    
    UIButton *doneButton = [UIButton new];
    [doneButton setTitle:@"确定" forState:UIControlStateNormal];
    [doneButton setTitleColor:RGB(88, 88, 88) forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    doneButton.layer.masksToBounds = YES;
    doneButton.layer.borderWidth = 1.0;
    doneButton.layer.borderColor = [RGB(88, 88, 88) CGColor];
    doneButton.layer.cornerRadius = 5.0;
    [doneButton addTarget:self action:@selector(clickDoneButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_account_field.mas_bottom).mas_offset(10);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-10);
        make.width.mas_equalTo(60);
    }];
}

- (void)clickDoneButton:(UIButton *)button
{
    [_account_field resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(clickTrueWithAccount:)]) {
        [self.delegate clickTrueWithAccount:_account_field.text];
    }
}

- (void)showSearchView:(id<SearchFriendViewDelegate>)delegate
{
    self.delegate = delegate;
    _account_field.text = nil;
    [_account_field becomeFirstResponder];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)hideSearchView
{
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (self) {
        [self hideSearchView];
    }
}

/**
 注册键盘弹出通知
 */
- (void)registKeyBoardNotofication
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 1.5, SCREEN_HEIGHT / 3));
        make.centerX.mas_equalTo(self);
        make.bottom.mas_equalTo(-keyboardRect.size.height - 20);
    }];
}

- (void)keyboardWillBeHidden:(NSNotification *)notification
{
    [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 1.5, SCREEN_HEIGHT / 3));
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    [UIView animateWithDuration:0.5 animations:^{
        [self layoutIfNeeded];
    }];
}


@end
